<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :offset="0" :xs="0"></el-col>
      <el-col :span="12" :offset="0" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到系统</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="isLoading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElNotification, FormRules } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
//引入小仓库
import useUserStore from '@/store/modules/user.ts'
import { getTime } from '@/utils/time.ts'

//定义路由器
let $router = useRouter()
//获取路由对象
let $route = useRoute()

//使用小仓库
let userStore = useUserStore()

//用户名和密码
let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
//button按钮加载状态
let isLoading = ref(false)
//form表单实例
let loginForms = ref()

//正则方法验证表单 _占位
const checkUserName = (_rule: any, value: any, callback: any) => {
  // console.log(rule);
  if (!/^[A-Za-z0-9\u4e00-\u9fa5]+$/g.test(value)) {
    console.log(123)

    callback(new Error('不能为特殊字符'))
    // callback('不能为特殊字符')
  } else {
    callback()
  }
}

//定义校验规则
let rules = reactive<FormRules<any>>({
  username: [
    { required: true, message: '必填', trigger: 'blur' },
    { min: 2, max: 7, message: '长度不对', trigger: 'blur' },
    { validator: checkUserName, trigger: 'blur' },
  ],
  password: [
    { required: true, message: '必填', trigger: 'blur' },
    { min: 2, max: 7, message: '长度不对', trigger: 'blur' },
  ],
})

//登录按钮登录
const login = async () => {
  //校验输入框
  let formCheck: boolean = await loginForms.value.validate()
  if (!formCheck) return
  isLoading.value = true
  try {
    await userStore.userLogin(loginForm)
    ElNotification({
      title: `${getTime()}好，登录成功`,
      // message: `${error}`,
      type: 'success',
      duration: 1000,
    })
    //判断登录时是否有query参数
    let redirect: any = $route.query.redirect
    isLoading.value = false
    //登录
    $router.push({ path: redirect || '/' })
    console.log(userStore.token, '@@@')
  } catch (error) {
    ElNotification({
      title: '登录失败',
      message: `${error}`,
      type: 'error',
      duration: 1000,
    })
    isLoading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
